#!/usr/bin/perl
# wiki / ideas / bugs to http://code.google.com/p/svnc

package svnc;

$VERSION = "0.1";
%SVNC = (
	author      => 'Gareth Stones',
	contact     => 'flin.gs@gmail.com',
	name        => 'svnc',
	description => 'SVN commit md5 hashing client',
	url         => 'http://code.google.com/p/svnc',
	license     => 'GNU/GPL v2'
);

use Digest::MD5;
use strict;

##############################
# core handler - constructor #
##############################

sub new () {
	
	my $class = shift;
	my $self = { _ext = qw(),
				 _path = './',
				 _file = undef
	};
	($self->{_path}, $self->{_ext}) = &arguments;
	bless ($self, $class);
	return $self;
}

sub usage () {
	
	print "usage: $0 (options)\n\n";
	print "OPTIONS\n";
	print "--path=STRING             Path of local repository\n";
	print "--extensions=ext,ext,...  Extensions to include within the scan\n";
	print "                          (seperated by commas)\n";
	print "wiki / ideas / bugs to http://code.google.com/p/svnc\n";
	exit -1;
}

######################################
# display instance variable contents #
######################################

sub display () {
	
	my $self = shift;
	
	print $self->{_path} . "\n";
}

##################
# parse cli argv #
##################

sub arguments () {
	
	my $self = shift;
	
	return &usage if $#ARGV eq -1;
	
	foreach (@ARGV) {
		if ($_ =~ /^--path=/) {
			s/--path=//;
			$self->{_path} = $_;
		}
		
		elsif ($_ =~ /^--extensions=/) {
			s/--extensions=//;
			@_ = split /\,/, $_;
			foreach (@_) {
				push $self->{_ext}, $_;
			}
		}
		
		else {
			&usage;
		}
	}
	
	return ($self->{_path}, $self->{_ext});
}

#############################################
# validate and opendir $path -d (recursive) #
#############################################

sub read () {
	
	my ($path, @ext) = @_;
	&usage unless -d $path;
	$path .= '/' if $path =~ /^([a-zA-Z0-9\_\.\/]+)(^\/)$/;
	opendir(DIR, $path) or &usage;
	my @dir = grep { /^[a-zA-Z0-9]+/ } readdir(DIR);
	
	foreach (@dir) {
		# -d, therefore, recursively read -d contents
		&read($path . $_) if -d $path . $_;
		# -f, therefore, extension match check etc
		print "&match($path . $_, @ext)\n" if -e $path;
		&match($path . $_, @ext) if -f $path;
	}
}

###############################################
# match file extension with files in $path -d #
###############################################

sub match () {
	
	my ($path, @ext) = @_;
	#no longer needed, -f check in &read
	#return 0 unless -f $path;
	
	foreach (@ext) {
		# +1 inc .
		if (substr($path, length $path - (length $_ + 1), length $path) =~ /$_/) {
			print "parse($path)\n";
			&parse($path, $_);
		}
	
		else {
			print "looks like we have a problem: ", substr($path, length $path - (length $_ + 1), length $path), "\n";
		}
	}
	
	return 1;
}

################################
# get commit message from file #
################################

sub parse () {
	
	my ($file, $ext) = @_;
	open(FILE, $file);
	while (<FILE>) {
		if ($_ =~ /\s\*\s\@commit\s/) {
			s/\s\*\s\@commit\s//g;
			s/\n//;
			print "svn commit $file -m \"$_\"\n";
			# we should send it to a checker, to check the md5 hash
		}
	}
	
	close(FILE);
	
	#print "$commit\n";
}

####################################
# check for md5 hash in ~/.svnc_db #
####################################

sub db () {
	
	my ($file, $message) = @_;
	open(HASH, $file) or die "Error: Could not open file!\n";
	my $hash = Digest::MD5->new->addFile(*HASH)->hexdigest;
	print $hash;
	use DBI;
	my $dbh = DBI->connect("dbi:SQLite:dbname=svnc", "", "", { RaiseError => 1 }) or die "Error: SQLite connection failure.\n";
	
	open(DB, '~/.svnc_db');
	
}

1;
